package com.pm.web.controller.projectManage;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pm.buglist.domain.Project;
import com.pm.common.OAService;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.domain.entity.ProjectListNodeMember;
import com.pm.common.core.domain.entity.ProjectTempleteVersion;
import com.pm.common.core.page.TableDataInfo;
import com.pm.common.enums.BusinessType;
import com.pm.common.enums.ProjectListStatusEnum;
import com.pm.project.domain.ProjectList;
import com.pm.project.domain.ProjectListNode;
import com.pm.project.domain.ProjectModifyRecord;
import com.pm.project.domain.ProjectModifyRecordDetail;
import com.pm.project.domain.vo.ProjectListMemberVO;
import com.pm.project.domain.vo.ProjectListVO;
import com.pm.project.domain.vo.ProjectTempleteVersionVO;
import com.pm.project.mapper.ProjectModifyRecordMapper;
import com.pm.project.service.ProjectListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/project/list")
public class ProjectListController  extends BaseController {

    @Resource
    ProjectListService projectListService;

    @Resource
    OAService oaService;

    @Resource
    ProjectModifyRecordMapper projectModifyRecordMapper;



    @GetMapping("/list")
    @Log(title = "项目:查询项目列表",businessType = BusinessType.OTHER)
    public TableDataInfo list(ProjectList projectList)
    {
        startPage();
        List<ProjectListVO> list = projectListService.list(projectList);
        return getDataTable(list);
    }


    @GetMapping("/detail")
    @Log(title = "项目:查询项目详情",businessType = BusinessType.OTHER)
    public AjaxResult detail(String id)
    {
        return  projectListService.detail(id);
    }


    @Log(title = "项目:创建项目",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('projectManage:list:add')")
    public AjaxResult add(@RequestBody ProjectList projectList){
        projectList.setStatus(ProjectListStatusEnum.CREATE.getStatus());
        return projectListService.add(projectList);
    }


    @Log(title = "项目:项目保存草稿",businessType = BusinessType.INSERT)
    @PostMapping("/draft")
    @PreAuthorize("@ss.hasPermi('projectManage:list:add')")
    public AjaxResult draft(@RequestBody ProjectList projectList){
        return projectListService.draft(projectList);
    }


    @Log(title = "项目:修改项目信息",businessType = BusinessType.UPDATE)
    @PostMapping("/modify")
    @PreAuthorize("@ss.hasPermi('projectManage:list:update')")
    public AjaxResult modify(@RequestBody ProjectList projectList){
        return projectListService.modify(projectList);
    }


    @Log(title = "项目:项目时间变更",businessType = BusinessType.UPDATE)
    @PostMapping("/modifyProject")
    @PreAuthorize("@ss.hasPermi('projectManage:list:update')")
    public AjaxResult modifyProject(@RequestBody ProjectModifyRecord projectModifyRecord) throws Exception {
        //判断是否已经有在审核中的变更记录 如有则要等上一个变更流程走完才能再走
        QueryWrapper<ProjectModifyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_list_id",projectModifyRecord.getProjectListId());
        queryWrapper.lt("status",10);
        List<ProjectModifyRecord> list = projectModifyRecordMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            List<JSONObject> processList =  projectListService.queryDiscardProcess(list);
            if(!CollectionUtils.isEmpty(processList)){
                return AjaxResult.error("还有在审核中的变更流程 请等流程走完再提流程!");
            }
        }
        return projectListService.modifyProject(projectModifyRecord);
    }


    @Log(title = "项目:OA项目变更流程数据修改",businessType = BusinessType.UPDATE)
    @PostMapping("/updateModifyProject")
    @PreAuthorize("@ss.hasPermi('projectManage:list:update')")
    public AjaxResult updateModifyProject(@RequestBody ProjectModifyRecord projectModifyRecord) throws Exception {
        return projectListService.updateModifyProject(projectModifyRecord);
    }


    @Log(title = "项目:查询项目变更记录详情",businessType = BusinessType.UPDATE)
    @PostMapping("/queryProjectModifyRecordDetail")
    public AjaxResult queryProjectModifyRecordDetail(@RequestBody ProjectModifyRecord projectModifyRecord)  {

        List<ProjectModifyRecordDetail> res = projectListService.queryProjectModifyRecordForDetail(projectModifyRecord);
         return AjaxResult.success(res);
    }



    @GetMapping("/queryProjectModifyRecord")
    @Log(title = "项目:查询项目变更记录",businessType = BusinessType.OTHER)
    public TableDataInfo queryProjectModifyRecord(ProjectList projectList)
    {


        startPage();
        List<ProjectModifyRecord> list =  projectListService.queryProjectModifyRecord(projectList);
        TableDataInfo res =  getDataTable(list);
        if(!CollectionUtils.isEmpty(res.getRows())) {
            for (Object row : res.getRows()) {
                ProjectModifyRecord projectModifyRecord = (ProjectModifyRecord)row;
                List<ProjectModifyRecordDetail> details =  projectListService.queryProjectModifyRecordDetail(projectModifyRecord);
                projectModifyRecord.setProjectModifyRecordDetailList(details);

                String oaStatus = oaService.queryProcessStatus(projectModifyRecord.getOaId());
                if("00".equals(oaStatus)){//废弃
                    projectModifyRecord.setStatus(-1);
                }else if("11".equals(oaStatus)){//驳回
                    projectModifyRecord.setStatus(2);
                }
            }
        }
        return res;
    }


    @Log(title = "项目:删除项目",businessType = BusinessType.UPDATE)
    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('projectManage:update:delete')")
    public AjaxResult delete(@RequestBody ProjectList projectList){
        return projectListService.delete(projectList);
    }


    @Log(title = "项目:项目中止",businessType = BusinessType.UPDATE)
    @PostMapping("/stop")
    @PreAuthorize("@ss.hasPermi('projectManage:list:stop')")
    public AjaxResult stop(@RequestBody ProjectList projectList){
        return projectListService.stop(projectList);
    }


    @Log(title = "项目:重启中止",businessType = BusinessType.UPDATE)
    @PostMapping("/restart")
    @PreAuthorize("@ss.hasPermi('projectManage:list:stop')")
    public AjaxResult restart(@RequestBody ProjectList projectList){
        return projectListService.restart(projectList);
    }

    @GetMapping("/getSetupDetail")
    @Log(title = "项目:查询项目任务详情",businessType = BusinessType.OTHER)
    public AjaxResult getSetupDetail(Long projectListId)
    {
        return  projectListService.getSetupDetail(projectListId);
    }

    @Log(title = "项目:删除项目节点",businessType = BusinessType.UPDATE)
    @PostMapping("/del/node")
    @PreAuthorize("@ss.hasPermi('projectManage:list:delNode')")
    public AjaxResult deleteNode(@RequestBody ProjectListNode projectListNode){
        return projectListService.deleteNode(projectListNode);
    }



    @GetMapping("/getSetup")
    @Log(title = "项目:查询项目任务",businessType = BusinessType.OTHER)
    public AjaxResult getSetup(Long projectListId)
    {
        return  projectListService.getSetup(projectListId);
    }



    @GetMapping("/getSetupDetailDraft")
    @Log(title = "项目:查询项目设置的草稿",businessType = BusinessType.OTHER)
    public AjaxResult getSetupDetailDraft(Long projectListId)
    {
        return  projectListService.getSetupDetailDraft(projectListId);
    }



    @Log(title = "项目:设置项目任务节点",businessType = BusinessType.INSERT)
    @PostMapping("/saveSetUp")
    @PreAuthorize("@ss.hasPermi('projectManage:list:setup')")
    public AjaxResult saveSetUp(@RequestBody JSONObject param){
        return projectListService.saveSetUp(param);
    }


    @Log(title = "项目:驳回项目",businessType = BusinessType.UPDATE)
    @PostMapping("/rejectSetUp")
    @PreAuthorize("@ss.hasPermi('projectManage:list:setup')")
    public AjaxResult rejectSetUp(@RequestBody JSONObject param){
        return projectListService.rejectSetUp(param);
    }

    @Log(title = "项目:项目经理驳回项目到设置项目环节",businessType = BusinessType.UPDATE)
    @PostMapping("/rejectProject")
    @PreAuthorize("@ss.hasPermi('projectManage:list:settimemember')")
    public AjaxResult rejectProject(@RequestBody JSONObject param){
        return projectListService.rejectProject(param);
    }


    @GetMapping("/setup/list")
    @Log(title = "项目:查询项目列表",businessType = BusinessType.OTHER)
    public TableDataInfo setupList(ProjectList projectList)
    {
        startPage();
        List<ProjectListVO> list = projectListService.setupList(projectList);
        return getDataTable(list);
    }


    @Log(title = "项目:设置项目时间和责任人",businessType = BusinessType.INSERT)
    @PostMapping("/saveSettimeMember")
    @PreAuthorize("@ss.hasPermi('projectManage:list:settimemember')")
    public AjaxResult saveSettimeMember(@RequestBody JSONObject param){
        return projectListService.saveSettimeMember(param);
    }


    @Log(title = "项目:设置项目时间和责任人保存草稿",businessType = BusinessType.INSERT)
    @PostMapping("/saveSettimeMemberDraft")
    @PreAuthorize("@ss.hasPermi('projectManage:list:settimemember')")
    public AjaxResult saveSettimeMemberDraft(@RequestBody JSONObject param){
        return projectListService.saveSettimeMemberDraft(param);
    }


    @GetMapping("/queryMilestone")
    @Log(title = "项目:查询项目里程碑",businessType = BusinessType.OTHER)
    public AjaxResult queryMilestone(ProjectList projectList)
    {
        JSONObject res = projectListService.queryMilestone(projectList);
        return AjaxResult.success(res);
    }


    @GetMapping("/queryProgress")
    @Log(title = "项目:查询项目进度",businessType = BusinessType.OTHER)
    public AjaxResult queryProgress(ProjectList projectList)
    {
        JSONObject res = projectListService.queryProgress(projectList);
        return AjaxResult.success(res);
    }



    @GetMapping("/totalProjectForUser")
    @Log(title = "项目:首页个人用户项目汇总",businessType = BusinessType.OTHER)
    public AjaxResult totalProjectForUser(Integer userFlag)
    {
        return projectListService.totalProjectForUser(userFlag);

    }

    /**
     * 查询未开始BUGLIST的项目列表
     * @param projectList
     * @return
     */
    @GetMapping("/queryProjectListNoBug")
    @Log(title = "项目:查询未开始BUGLIST的项目列表",businessType = BusinessType.OTHER)
    public TableDataInfo queryProjectListNoBug(ProjectList projectList){
        startPage();
        List<ProjectListVO> list = projectListService.queryProjectListNoBug(projectList);
        return getDataTable(list);
    }


    /**
     * 查询是否是第一任务
     * @param projectListNodeMemberId
     * @return
     */
    @GetMapping("/queryTaskIndex")
    @Log(title = "项目:查询是否是第一任务",businessType = BusinessType.OTHER)
    public AjaxResult queryTaskIndex(Long projectListNodeMemberId){
        return projectListService.queryTaskIndex(projectListNodeMemberId);
    }


    /**
     * 查询项目操作日志
     * @param projectListId
     * @return
     */
    @GetMapping("/log")
    @Log(title = "项目:查询项目操作日志",businessType = BusinessType.OTHER)
    public AjaxResult listLog(Long projectListId)
    {
        List<JSONObject> list = projectListService.listLog(projectListId);
        return AjaxResult.success(list);
    }


    /**
     * 项目经理修改项目责任人
     * @param projectListNodeMember
     * @return
     */
    @PostMapping("/modifyTaskUser")
    @Log(title = "项目:项目经理修改项目责任人",businessType = BusinessType.OTHER)
    public AjaxResult modifyTaskUser(@RequestBody ProjectListNodeMember projectListNodeMember){
        return projectListService.modifyTaskUser(projectListNodeMember);
    }


    @Log(title = "项目:添加项目成员",businessType = BusinessType.INSERT)
    @PostMapping("/addProjectMember")
    public AjaxResult addProjectMember(@RequestBody ProjectListMemberVO projectListMemberVO){
        return projectListService.addProjectMember(projectListMemberVO);
    }

    @Log(title = "项目:替换项目成员",businessType = BusinessType.INSERT)
    @PostMapping("/replaceProjectMember")
    public AjaxResult replaceProjectMember(@RequestBody ProjectListMemberVO projectListMemberVO){
        return projectListService.replaceProjectMember(projectListMemberVO);
    }


}
